// PlugIn.cpp - demonstration plugin for 4NT / TC 7.0

#define UNICODE 1
#define _UNICODE 1

#include <stdio.h>
#include <tchar.h>

#include <windows.h>

#include "PlugIn.h"
#include "TakeCmd.h"


BOOL APIENTRY DllMain( HANDLE hModule, DWORD  dwReason, LPVOID lpReserved )
{
	switch( dwReason )	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
	}
	return TRUE;
}


DLLExports LPPLUGININFO WINAPI GetPluginInfo( void )
{
	static PLUGININFO piInfo;

	piInfo.pszDll = (LPTSTR)L"T";
	piInfo.pszAuthor = (LPTSTR)L"Paul Moore";
	piInfo.pszEmail = (LPTSTR)L"pf_moore@yahoo.co.uk";
	piInfo.pszWWW = (LPTSTR)L"http://www.arkenstone.demon.co.uk";
	piInfo.pszDescription = (LPTSTR)L"Test plugin";
	piInfo.pszFunctions = (LPTSTR)L"test,@test";
	piInfo.nMajor = 1;
	piInfo.nMinor = 0;
	piInfo.nBuild = 1;

	return &piInfo;
}


DLLExports BOOL WINAPI InitializePlugin( void )
{
	return 0;
}


DLLExports BOOL WINAPI ShutdownPlugin( BOOL bEndProcess )
{
	return 0;
}

// this is an internal command called from 4NT / TC
DLLExports INT WINAPI f_test (LPTSTR lpszString)
{
    LPTSTR arg;
    TCHAR buf[2000];
    int i;

    for (i = 0; ; ++i) {
	TCHAR *nthptr;
	LPTSTR nth = NthArgument(lpszString, i, buf, &nthptr);
	if (!nth || !*nth) break;
	Printf(L"Arg %d: %s\n", i, nth);
    }
    lstrcpy(lpszString, L"... Result");
    return 0;
}

BOOL WINAPI MyBreakHandler (DWORD type)
{
    Printf(L"Break!\n");
    return TRUE;
}

DLLExports INT WINAPI test (LPTSTR lpszString)
{
    LPTSTR arg;
    TCHAR buf[2000];
    int i;

    for (i = 0; ; ++i) {
	TCHAR *nthptr;
	LPTSTR nth = NthArgument(lpszString, i, buf, &nthptr);
	if (!nth || !*nth) break;
	/* Note: Quoted args have " at start and end. Strip manually. */
	StripEnclosingQuotes(nth);
	Printf(L"Arg %d: '%s' (nthptr = '%s')\n", i, nth, nthptr);
    }

    Printf(L"Command line: '%s'\n", lpszString);
    Printf(L"NextArgument(s,0)\n");
    Printf(L"Ret val: '%s'\n", NextArgument(lpszString, 0));
    Printf(L"Command line: '%s'\n", lpszString);
    Printf(L"NextArgument(s,1)\n");
    Printf(L"Ret val: '%s'\n", NextArgument(lpszString, 1));
    Printf(L"Command line: '%s'\n", lpszString);
    Printf(L"GetEnv(A) = %s\n", GetEnvironmentVariablePtr(L"A"));
    Printf(L"GetEnv(B) = %s\n", GetEnvironmentVariablePtr(L"B"));
#if 0
    for (i = 0; i < 10; ++i) {
	DWORD n;
	int ret;
	//ret = ReadConsole(GetStdHandle(STD_INPUT_HANDLE), buf, 5, &n, 0);
	//SetConsoleCtrlHandler(MyBreakHandler, TRUE);
	HoldSignals();
	ret = GetLine(GetStdHandle(STD_INPUT_HANDLE), buf, 5, 0x10000);
	EnableSignals();
	//SetConsoleCtrlHandler(MyBreakHandler, FALSE);
	Printf(L"Got ret %d, val: %s\n", ret, buf);
    }
#endif
    return 0;
}
